package uk.co.biggroup.lotto.syndicate.dataset;

import java.io.Serializable;
import java.util.ArrayList;

import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import uk.co.biggroup.lotto.syndicate.Logh;

public class DataDraw implements Serializable {
	//<draw id='74' order='4' date='09 Jun 2012' status='Finished' type='Lotto' result='5,14,24,26,37,44,30'>
	private static final long serialVersionUID = 7526472295611776147L;
	private int id;
	@SuppressWarnings("unused")
	private int order;
	private int typeInt;
	private String date;
	private String status;
	private String type;
	private String result;
	private int[] resultArr;
	private ArrayList<DataLine> lines;
//	private DataLine[] lines;
	static int drawCounter = 1;
	
//	String NUMBERS_XPATH = "//draws/draw[@id='6']/lines//line/@numbers";
	
	public DataDraw(TagNode node, int id, int order, String date, String status, String type, String result) {
		this.id = id;
		this.order = order;
		this.date = date;
		this.status = status;
		this.type = type;
		if(type.equalsIgnoreCase("Lotto")) {
			this.typeInt = 1;
			if (result.length() == 0) {
				this.setResult("0,0,0,0,0,0,0");
			} else {
				this.setResult(result);
			}
		} else if (type.equalsIgnoreCase("EuroMillions")) {
			this.typeInt = 2;
			if (result.length() == 0) {
				this.setResult("0,0,0,0,0,0,0");
			} else {
				this.setResult(result);
			}
		} else if (type.equalsIgnoreCase("HealthLottery")) {
			this.typeInt = 3;
			if (result.length() == 0) {
				this.setResult("0,0,0,0,0");
			} else {
				this.setResult(result);
			}
		} else {
			this.typeInt = 1;
			if (result.length() == 0) {
				this.setResult("0,0,0,0,0,0,0");
			} else {
				this.setResult(result);
			}
		}
		try {
			this.getLines(node);
		} catch (XPatherException e) {
			Logh.i("XPatherException");
		}
	}	
	
	private void getLines(TagNode node) throws XPatherException {
		Object[] nodes_numbers = node.evaluateXPath(this.getNumbersXpath());
		Object[] nodes_raffle = node.evaluateXPath(this.getRaffleXpath());
		int objects = nodes_numbers.length;
		lines = new ArrayList<DataLine>(objects);
		for(int i = 0; i <= (objects - 1); i++ ) {
			DataLine line = (DataLine) DataLineFactory.getDataLine(this.type, nodes_numbers[i].toString(), nodes_raffle[i].toString(), this.result);
			lines.add(line);
		}
		
	}
	
	private String getNumbersXpath() {
		return "//draws/draw[@id='" + this.id + "']/lines//line/@numbers";
	}

	private String getRaffleXpath() {
		return "//draws/draw[@id='" + this.id + "']/lines//line/@raffle";
	}
	
	public void setId(int setId) {
		id = setId;
	}
	
	public void setStatus(String attStatusset) {
		status = attStatusset;
	}
	
	public void setType(int attType) {
		type = Integer.toString(attType);
	}

	public void setType(String attType) {
		type = attType;
	}
	
	public void setDate(String attDate) {
		date = attDate;
	}

	public void setResult(String attResult) {
		result = attResult;
		String[] temp = attResult.split(",");
		this.resultArr = new int[temp.length];
		for (int i = 0; i < temp.length; i++) {
			this.resultArr[i] = Integer.parseInt(temp[i]);
		}
	}
	
	public int getId()
	{
		return id;
	}

	public int getType()
	{
		return typeInt;
	}

	public String getDate()
	{
		return date;
	}
	
	public String getStatus()
	{
		return status;
	}
	
	public String getResult()
	{
		return result;
	}

	public String getResultSingle(int index) {
		String val;
		if (resultArr[index] < 10) {
				val = "0" + Integer.toString(resultArr[index]);
			} else {
				val = Integer.toString(resultArr[index]);
			}
		return val;
	}
	
	public int getNoResultNumbers() {
		return resultArr.length;
	}
	
	public int getCounter()
	{
		return drawCounter;
	}
	
	public ArrayList<DataLine> getDataLines() {
		return this.lines;
	}
	
	public int compareTo(DataDraw obj) {
		DataDraw tmp = (DataDraw) obj;
		if (this.getId() < tmp.getId()) {
			/* instance lt received */
			return -1;
		} else if (this.getId() > tmp.getId()) {
			/* instance gt received */
			return 1;
		}
		/* instance == received */
		return 0;
	}
	
}
